package cn.b.sky.mes.andon.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.andon.model.AndonType;
import cn.b.sky.mes.andon.model.AndonTypeAttribute;
import cn.b.sky.mes.andon.model.condition.AndonTypeCondition;
import cn.b.sky.mes.andon.service.AndonTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

/**
 * 
 * <strong>Title : AndonTypeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.andon.controller<br></strong>
 * <strong>Description : </strong>ANDON类别<br> 
 * <strong>Create on : 2016-9-14 上午9:12:50<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/andonType/*")
public class AndonTypeController extends SecurityController {

  @Resource
  AndonTypeService andonTypeService;

  @Resource
  ECodeService eCodeService;

  @RequestMapping("andonTypeList")
  public String andonTypeList(Model model, @ModelAttribute("condition") AndonTypeCondition condition, String navTabId) {
    condition.setSiteCode(getSiteCode());
    List<AndonType> list = andonTypeService.selectandonTypeListByCondition(condition);
    int count = andonTypeService.selectandonTypeListCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "andon/andonType/andonType_list";
  }
  
  @RequestMapping("andonTypeLookUp")
  public String andonTypeLookUp(Model model, @ModelAttribute("condition") AndonTypeCondition condition, String navTabId) {
    condition.setSiteCode(getSiteCode());
    List<AndonType> list = andonTypeService.selectandonTypeListByCondition(condition);
    int count = andonTypeService.selectandonTypeListCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "andon/andonType/andonType_lookUp";
  }


  @RequestMapping("viewAndonType")
  public String viewAndonType(String navTabId, Model model, String code) {
    AndonType andonType = andonTypeService.getAndonType(this.getSiteCode(), code);
    model.addAttribute("andonType", andonType);
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.ANDONATTRIBUTETYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("ECode", ecode);
    return "andon/andonType/andonType_view";
  }

  @RequestMapping("addAndonType")
  public String addAndonType(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    return "andon/andonType/andonType_add";
  }

  @RequestMapping("modifyAndonType")
  public String modifyAndonType(String navTabId, Model model, String code) {
    AndonType andonType = andonTypeService.getAndonType(this.getSiteCode(), code);
    model.addAttribute("andonType", andonType);
    model.addAttribute("pattern", "modify");
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.ANDONATTRIBUTETYPE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("ECode", ecode);
    return "andon/andonType/andonType_add";
  }

  @RequestMapping("validateAndonType")
  @ResponseBody
  public boolean validateAndonType(String andonTypeCode) {
    if (StringUtils.isBlank(andonTypeCode)) {
      return true;
    } else {
      AndonType andonType = andonTypeService.getAndonType(this.getSiteCode(), andonTypeCode.toUpperCase());
      return andonType == null;
    }
  }

  @RequestMapping("saveAndonType")
  @ResponseBody
  public DWZResult saveAndonType(String id, String ids, AndonType andonType, String navTabId, Model model) {
    if (null == andonType) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    andonType.setSiteCode(this.getSiteCode());
    if (StringUtils.isNotBlank(ids)) {
      String[] andonAttributes = ids.split(",");//作业单元id数组
      if (andonAttributes.length > 0) {
        for (int i = 0; i < andonAttributes.length; i++) {
          AndonTypeAttribute attr = new AndonTypeAttribute(getSiteCode(), andonType.getAndonTypeCode(),
              andonAttributes[i]);
          setCreateInfo(attr);
          andonType.getAndonAttributes().add(attr);
        }
      }
    }
    if (StringUtils.isBlank(id)) {
      setCreateInfo(andonType);
      andonTypeService.insertAndonType(andonType);
    } else {
      setModifyInfo(andonType);
      andonTypeService.updateAndonType(andonType);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deleteAndonType")
  @ResponseBody
  public DWZResult deleteAndonType(String navTabId, Model model, String codes, HttpServletRequest request) {
    String[] idArr = codes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String code = idArr[i];
      String message = null;
      message = andonTypeService.deleteAndonType(this.getSiteCode(), code);
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

  @RequestMapping("disableAndonType")
  @ResponseBody
  public DWZResult disableAndonType(String navTabId, Model model, String codes, HttpServletRequest request) {
    String[] idArr = codes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String code = idArr[i];
      String message = null;
      message = andonTypeService.disableAndonType(this.getSiteCode(), code);
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

  @RequestMapping("enableAndonType")
  @ResponseBody
  public DWZResult enableAndonType(String navTabId, Model model, String codes, HttpServletRequest request) {
    String[] idArr = codes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String code = idArr[i];
      String message = null;
      message = andonTypeService.enableAndonType(this.getSiteCode(), code);
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }
}
